package handler

import (
	"context"
	"gitee.com/wendyRose/common"
	"gitee.com/wendyRose/payment/domain/model"
	"gitee.com/wendyRose/payment/domain/service"
	"gitee.com/wendyRose/payment/proto/payment"
)

type Payment struct{
	PaymentDataService service.IPaymentDataService
}

func (e *Payment) AddPayment(ctx context.Context, request *payment.PaymentInfo, response *payment.PaymentID) error  {
	newPayment := &model.Payment{}
	if err := common.SwapTo(request, newPayment); err != nil {
		common.Error(err)
	}
	paymentId, err := e.PaymentDataService.AddPayment(newPayment)
	if err != nil {
		common.Error(err)
	}
	response.PaymentId = paymentId
	return nil
}

func (e *Payment) UpdatePayment(ctx context.Context, request *payment.PaymentInfo, response *payment.Response) error {
	updatePayment := &model.Payment{}
	if err := common.SwapTo(request, updatePayment); err != nil {
		common.Error(err)
	}
	return e.PaymentDataService.UpdatePayment(updatePayment)
}

func (e *Payment) DeletePaymentByID(ctx context.Context, request *payment.PaymentID, response *payment.Response) error {
	return e.PaymentDataService.DeletePaymentByID(request.PaymentId)
}

func (e *Payment) FindPaymentByID(ctx context.Context, request *payment.PaymentID, response *payment.PaymentInfo) error {
	payment, err := e.PaymentDataService.FindPaymentByID(request.PaymentId)
	if err != nil {
		common.Error(err)
	}
	return common.SwapTo(payment, response)
}

func (e *Payment) FindAllPayment(ctx context.Context, request *payment.All, response *payment.PaymentAll) error {
	paymentAll, err := e.PaymentDataService.FindAllPayment()
	if err != nil {
		common.Error(err)
	}
	for _,v := range paymentAll {
		paymentInfo := &payment.PaymentInfo{}
		if err := common.SwapTo(v, paymentInfo); err !=  nil {
			common.Error(err)
		}
		response.PaymentInfo = append(response.PaymentInfo, paymentInfo)
	}
	return nil
}


